package com.lln.cjcx.interceptor;
import kotlin.jvm.internal.Intrinsics;
import com.lln.cjcx.pojo.Message;
import com.lln.cjcx.pojo.MyOkHttpClient;
import com.lln.cjcx.value.CommonValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.web.servlet.ModelAndView;
import yfdc.JavaBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * @author LiLinnan
 * @version 1.0
 * @date 2020/1/22 20:27
 */
public final class GradeInterceptor extends M {
	public GradeInterceptor() {
		super();
	}
	public final String toString() {
		return "【成绩拦截器GradeInterceptor】@" + Integer.toHexString(hashCode());
	}
	@Override public final boolean preHandle(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @Nullable Object handler) {
		try {
			super.preHandle(request, response, handler);
		} catch (Throwable ignore) {
			//ignore
		}
		//System.out.println(this.toString() + this.preHandle_str());
		boolean rtn = true;
		try {
			Intrinsics.checkParameterIsNotNull(request, "request");
			Intrinsics.checkParameterIsNotNull(response, "response");
			HttpSession session = request.getSession();
			Intrinsics.checkNotNull(session, "session must not null");
			if (session.getAttribute(CommonValue.CLIENT_NAME_IN_SESSION) == null) {
				session.setAttribute(CommonValue.CLIENT_NAME_IN_SESSION, new MyOkHttpClient());
			}
			MyOkHttpClient client = (MyOkHttpClient) session.getAttribute(CommonValue.CLIENT_NAME_IN_SESSION);
			Intrinsics.checkNotNull(client, "client must not null");
			if (!client.isLogin()) {
				response.setContentType("text/html;charset=UTF-8");
				java.io.PrintWriter writer = null;
				try {
					writer = response.getWriter();
				} catch (Throwable ex) {
					//ignored and will never happens.
				}
				Intrinsics.checkNotNull(writer, "writer should not be null");
				if (writer != null) writer.println(yfdc.GsonFactory.getGson().toJson(new Message(-2, "请先登录")));
				rtn = false;
			}
		} catch (Throwable da) {
			da.printStackTrace(System.out);
			rtn = false;
		}
		return rtn;
	}

	@Override public final void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
		try {
			super.postHandle(request, response, handler, modelAndView);
		} catch (Throwable ignore) {
			//ignored
		}
		//System.out.println(this + postHandle_str());
	}

	@Override public final void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
		try {
			super.afterCompletion(request, response, handler, ex);
		} catch (Throwable ignore) {
			//ignored
		}
		if(ex!=null){
			try{
				Intrinsics.checkParameterIsNotNull(request,"request");
				Intrinsics.checkParameterIsNotNull(response,"response");
				java.io.Writer writer=response.getWriter();
				response.setContentType("application/json;charset=utf-8");
				response.setCharacterEncoding("utf-8");
				response.setStatus(200);
				response.sendRedirect("/logout");
				writer.write(yfdc.GsonFactory.getGson().toJson(new Message(-2, JavaBean.stackTrace(ex))));
			}catch (Throwable var100){
				//ignored
			}
		}
		//System.out.println(this + afterCompletion_str());
	}
}
